﻿
using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;


namespace WP09S03_OfficeTracking.Controller
{
    class Email
    {

          // All the fields that are needed
          private string mMailFrom;
          private string mMailTo;
          private string mMailSubject;
          private string mMailBody;
          private string mSMTPServer;
          private int mSMTPPort;
          private string mSMTPUsername;
          private string mSMTPPassword;
          private bool mSMTPSSL;
          private MailMessage MailObject;

          private Exception ex;
          private string exMsg;

          // Properties

          public Exception Exception {
              get { return ex; }
          }
          public string ErrorMessage {
              get { return exMsg; }
          }
          public string MailFrom { set { mMailFrom = value; }
                                   get { return mMailFrom; } }
          public string MailTo { set { mMailTo = value; }
                                 get { return mMailTo; } }
          public string MailSubject
              { set { mMailSubject = value; }
                get { return mMailSubject; } }
          public string MailBody { set { mMailBody = value; }
                                   get { return mMailBody; } }
          public string SMTPServer { set { mSMTPServer = value; }
                                     get { return mSMTPServer; } }
          public int SMTPPort { set { mSMTPPort = value; }
                                get { return mSMTPPort; } }
          public string SMTPUsername
              { set { mSMTPUsername = value; }
                get { return mSMTPUsername; } }
          public string SMTPPassword {
                set { mSMTPPassword = value; }
                get { return mSMTPPassword; } }
          public Boolean SMTPSSL { set { mSMTPSSL = value; }
                                   get { return mSMTPSSL; } }


          // Functions
          public Boolean Send()
          {
           // build the email message
              Boolean bTemp = true;

             
              try
              {

                   MailMessage Email = new MailMessage();
                   MailAddress MailFrom = 
                     new MailAddress(mMailFrom,mMailFrom);
                   Email.From = MailFrom;
                   Email.To.Add(mMailTo);

                   Email.Subject = mMailSubject;
                   Email.Body = mMailBody;
                   Email.IsBodyHtml = true;

                   // Smtp Client
                   SmtpClient SmtpMail = 
                    new SmtpClient(mSMTPServer,mSMTPPort);
                   SmtpMail.Credentials = 
                    new NetworkCredential(mSMTPUsername,mSMTPPassword);
                   SmtpMail.EnableSsl = mSMTPSSL;
                   SmtpMail.DeliveryMethod = SmtpDeliveryMethod.Network;


                    //non-blocking
                   SmtpMail.SendAsync(Email, mMailTo.ToString());
             
                     return true;
               }
               catch (Exception ex)
               {
                   this.ex = ex;
                   exMsg = ex.Message;
                 
                   bTemp = false;
               }
               return bTemp;
          }
                   
          // Constructor
          public Email()
                {

                    MailObject = new MailMessage();
                    mMailFrom = WP09S03_OfficeTracking.Properties.Settings.Default.MailFrom;
                    mMailTo = "";
                    mMailSubject = "";
                    mMailBody = "";
                    mSMTPServer = WP09S03_OfficeTracking.Properties.Settings.Default.SMTPServer ;
                    mSMTPPort = WP09S03_OfficeTracking.Properties.Settings.Default.SMTPPort ;
                    mSMTPUsername = WP09S03_OfficeTracking.Properties.Settings.Default.SMTPUsername ;
                    mSMTPPassword = WP09S03_OfficeTracking.Properties.Settings.Default.SMTPPassword ;
                    mSMTPSSL = WP09S03_OfficeTracking.Properties.Settings.Default.SMTPSSL ;
                }

        }

}